/**
 * Created by HIAPAD on 2017/10/30.
 */
var mysql=require("mysql");
var option=require("./config");
var logger=require("./logger");
var mode=require("../config").env;
function DB(){
    this.pool=mysql.createPool(option);
}
DB.prototype.query=function(sql,args,callBack){
    var sqlStr=sql;
    for(var i=0;i<args.length;i++){
        sqlStr=sqlStr.replace("?",args[i]);
    }
    this.pool.query(sql,args,function(err,result){
        if(err){
            logger.errorLog(err.toString());
        }
        callBack(err,result);
    })
    if(mode=="debug"){
        console.log(sqlStr);
    }else{
        logger.sqlLog(sqlStr);
    }
}
DB.prototype.close=function(){
    this.pool.close();
}

module.exports={
    createDB:function(){
        return new DB();
    }
}